package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbWhiteList;
import com.ruoyi.system.service.ITbWhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 白名单信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/business/white")
public class TbWhiteListController extends BaseController
{
    @Autowired
    private ITbWhiteListService tbWhiteListService;

    /**
     * 查询白名单信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbWhiteList tbWhiteList)
    {
        startPage();
        List<TbWhiteList> list = tbWhiteListService.selectTbWhiteListList(tbWhiteList);
        return getDataTable(list);
    }

    /**
     * 导出白名单信息列表
     */
    @Log(title = "白名单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbWhiteList tbWhiteList)
    {
        List<TbWhiteList> list = tbWhiteListService.selectTbWhiteListList(tbWhiteList);
        ExcelUtil<TbWhiteList> util = new ExcelUtil<TbWhiteList>(TbWhiteList.class);
        util.exportExcel(response, list, "白名单信息数据");
    }

    /**
     * 获取白名单信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbWhiteListService.selectTbWhiteListById(id));
    }

    /**
     * 新增白名单信息
     */
    @Log(title = "白名单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbWhiteList tbWhiteList)
    {
        TbWhiteList search = new TbWhiteList();
        search.setCard(tbWhiteList.getCard());
        List<TbWhiteList> list = tbWhiteListService.selectTbWhiteListList(search);
        if (list != null && list.size() > 0) {
            return error("您新增的车牌已经在白名单中存在，请确认后再输入！");
        }

        return toAjax(tbWhiteListService.insertTbWhiteList(tbWhiteList));
    }

    /**
     * 修改白名单信息
     */
    @Log(title = "白名单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbWhiteList tbWhiteList)
    {
        Long id = tbWhiteList.getId();
        TbWhiteList search = new TbWhiteList();
        search.setCard(tbWhiteList.getCard());
        List<TbWhiteList> list = tbWhiteListService.selectTbWhiteListList(search);
        if (list != null && list.size() > 0) {

            for (TbWhiteList item : list) {
                Long itemId = item.getId();
                if (id.intValue() != itemId.intValue()) {
                    return error("您修改的车牌已经在白名单中存在，请确认后再输入！");
                }
            }
        }

        return toAjax(tbWhiteListService.updateTbWhiteList(tbWhiteList));
    }

    /**
     * 删除白名单信息
     */
    @Log(title = "白名单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbWhiteListService.deleteTbWhiteListByIds(ids));
    }
}
